package com.leeh.jsontest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mainDrawerLayout;
    private LinearLayout totalLinearLayout;
    private ActionBarDrawerToggle mainDrawerToggle;
    private Toolbar mainToolbar;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
        totalLinearLayout = (LinearLayout) findViewById(R.id.total_linerLayout);
        mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
        mainToolbar.setTitle("");
        setSupportActionBar(mainToolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mainDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mainDrawerToggle = new ActionBarDrawerToggle(this, mainDrawerLayout, mainToolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                totalLinearLayout.setLeft((int) (drawerView.getWidth() * slideOffset));
            }
        };
        mainDrawerToggle.syncState();
        mainDrawerLayout.addDrawerListener(mainDrawerToggle);

        changeFragment(new TeacherFragment());

    }

    private void changeFragment(Fragment fragment) {

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_frame_layout, fragment);
        fragmentTransaction.commit();

    }

    public void teacherCourse_onClick(View view) {
        mainDrawerLayout.closeDrawer(GravityCompat.START, true);
        changeFragment(new TeacherFragment());
        //mainToolbar.setTitle("老师课表");
    }

    public void roomCourse_onClick(View view) {
        mainDrawerLayout.closeDrawer(GravityCompat.START, true);
        changeFragment(new RoomFragment());
        //mainToolbar.setTitle("教室课表");
    }

    public void courseCourse_onClick(View view) {
        mainDrawerLayout.closeDrawer(GravityCompat.START, true);
        changeFragment(new CourseFragment());
        //mainToolbar.setTitle("课程课表");
    }

    public void freeOptionCourse_onClick(View view) {
        mainDrawerLayout.closeDrawer(GravityCompat.START, true);
        changeFragment(new FreeOptionFragment());
        //mainToolbar.setTitle("任选课表");
    }
}
